angular.module('service.coupon', [
    "ngResource"
])
    .service('CouponService', CouponService);
CouponService.$inject = ['$resource'];
function CouponService ($resource) {
    //通过店铺id获取有效的活动
    this.getByshopid = function(id){
        var resource = $resource(privilege + 'coupon/shopid/effective/:shopid');
        return resource.query({shopid:id,status:0});
    };
    //离去优惠券
    this.saveBycouponid = function(receiver,success,error){
        var resource = $resource(privilege + 'couponreceiver');
        return resource.save(receiver,success,error)
    };
    //通过用户id和店铺id获取优惠券
    this.getByUidAndShopId = function(shopid,receiverid,success,error){
        var resource = $resource(privilege + 'couponreceiver/shopid/:shopid/:receiverid');
        return resource.query({shopid:shopid,receiverid:receiverid},success,error);
    };
    //判断可用优惠券
    this.validCoupon = function(shopid,userid,goodscontent,ValidNum_fun,couponList_fun){
        var _self = this;
        _self.getByUidAndShopId(shopid,userid,function (data) {
            var receiveCoupon = [];
            for (var i = 0; i < data.length; i++) {
                if (data[i].status == 0) {
                    data[i].usable = true;
                    data[i].checked = false;
                    data[i].limitCondition = "";
                    if (data[i].full >= goodscontent.paymentprice) {
                        data[i].limitCondition = "消费金额未满足条件";
                        data[i].usable = false;
                    }else if(data[i].discount>=goodscontent.paymentprice){
                        data[i].limitCondition = "折扣金额大于消费金额";
                        data[i].usable = false;
                    }
                    if(data[i].usable){
                        ValidNum_fun();
                    }
                    receiveCoupon.push(data[i]);
                }
            }
            _self.calcCoupon(goodscontent, receiveCoupon);
            couponList_fun(receiveCoupon);
            loadCart(false, "loader");
        }, function () {
            loadCart(false, "loader");
        })
    };
    //通过receiverid获取优惠券
    this.getByReceiverId = function(receiverid,status){
        var resource = $resource(privilege + 'couponreceiver/receiverid/:receiverid');
        return resource.query({receiverid:receiverid,status:status});
    };
    //通过couponid获取优惠券
    this.getByCouponId = function(couponid){
        var resource = $resource(privilege + 'coupon/:couponid');
        return resource.get({couponid:couponid});
    };
    //修改优惠券的使用状态
    this.saveByCouponIdAndRecId = function(receiver,success,error){
        var resource = $resource(privilege + 'couponreceiver/:couponid/:receiverid');
        return resource.save({couponid:receiver.couponid,receiverid:receiver.receiverid},receiver,success,error)
    };
    // 根据订单计算出优惠活动可享受的优惠列表
    this.calcCoupon = function(goodscontent, coupon_list) {
        var price = goodscontent.paymentprice
        var discount = filtOneActivity(goodscontent,coupon_list);
        goodscontent.couponid = discount.couponid;
        goodscontent.couponDiscount = discount.discount;
        goodscontent.paymentprice = price-discount.discount;
        return goodscontent;
    };
    //删除券
    this.delcouponBydoubleId = function(couponid,receiverid,success,error){
        var resource = $resource(privilege + 'couponreceiver/:couponid/:receiverid');
        return resource.remove({couponid:couponid,receiverid:receiverid},success,error);
    };
    //筛选可享受的活动
    function filtOneActivity(goodscontent, coupon_list) {
        var result = {
            discount:0,
            couponid:''
        };
        var total = goodscontent.paymentprice;
        coupon_list = coupon_list.sort(compare("discount"));
        //将优惠力度从小到大排序
        for (var i=0; i<coupon_list.length;i++) {
            var Rule = coupon_list[i];
            // 判断总金额条件
            if (Rule.full >= total||Rule.discount>=total) {
                continue;
            }
            // 判断总金额条件
            result=Rule;
        }
        result.checked = true;//当前优惠券为选中状态
        return result;
    };
    //从小到大排序
    function compare(property) {//比较函数
        return function (x,y){
            if (x[property] < y[property]) {
                return -1;
            } else if (x[property] > y[property]) {
                return 1;
            } else {
                return 0;
            }
        }
    }
}